package cn.wu.reggie.mapper;

import cn.wu.reggie.dto.DishDto;
import cn.wu.reggie.entity.Dish;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;

import java.util.List;

public interface DishMapper {
    @Select("select * from dish where category_id = #{categoryId}")
    List<Dish> findByCategoryId(Long categoryId);

    @Insert("insert into dish values(null,#{name},#{categoryId},#{price},#{code},#{image},#{description},#{status},#{sort},#{createTime},#{updateTime},#{createUser},#{updateUser},0)")
    //@Options(keyColumn = "id",keyProperty = "id",useGeneratedKeys = true) //仅适用于主键自增的数据库
    @SelectKey(statement = "select last_insert_id()",before = false,keyProperty = "id",keyColumn = "id",resultType = Long.class) //适用于所有的数据库
    void save(DishDto dishDto);

    List<Dish> findByPage(String name);

    @Select("select * from dish where id = #{id}")
    Dish findById(Long id);


    void update(DishDto dishDto);

    @Select("select * from dish where category_id = #{categoryId} and status = 1")
    List<Dish> findOnSaleByCategoryId(Long categoryId);

    @Select("select * from dish where id = #{id} and status = 0")
    Dish findStatusById(Long id,Integer status);
}
